<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>coordinate.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_math_coordinate.js.html">coordinate.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A utility class for representing two-dimensional positions.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.math.Coordinate&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.math&#39;);
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * Class for representing coordinates and positions.
<a name="line28"></a> * @param {number=} opt_x Left, defaults to 0.
<a name="line29"></a> * @param {number=} opt_y Top, defaults to 0.
<a name="line30"></a> * @constructor
<a name="line31"></a> */
<a name="line32"></a>goog.math.Coordinate = function(opt_x, opt_y) {
<a name="line33"></a>  /**
<a name="line34"></a>   * X-value
<a name="line35"></a>   * @type {number}
<a name="line36"></a>   */
<a name="line37"></a>  this.x = goog.isDef(opt_x) ? opt_x : 0;
<a name="line38"></a>
<a name="line39"></a>  /**
<a name="line40"></a>   * Y-value
<a name="line41"></a>   * @type {number}
<a name="line42"></a>   */
<a name="line43"></a>  this.y = goog.isDef(opt_y) ? opt_y : 0;
<a name="line44"></a>};
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>/**
<a name="line48"></a> * Returns a new copy of the coordinate.
<a name="line49"></a> * @return {!goog.math.Coordinate} A clone of this coordinate.
<a name="line50"></a> */
<a name="line51"></a>goog.math.Coordinate.prototype.clone = function() {
<a name="line52"></a>  return new goog.math.Coordinate(this.x, this.y);
<a name="line53"></a>};
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>if (goog.DEBUG) {
<a name="line57"></a>  /**
<a name="line58"></a>   * Returns a nice string representing the coordinate.
<a name="line59"></a>   * @return {string} In the form (50, 73).
<a name="line60"></a>   * @override
<a name="line61"></a>   */
<a name="line62"></a>  goog.math.Coordinate.prototype.toString = function() {
<a name="line63"></a>    return &#39;(&#39; + this.x + &#39;, &#39; + this.y + &#39;)&#39;;
<a name="line64"></a>  };
<a name="line65"></a>}
<a name="line66"></a>
<a name="line67"></a>
<a name="line68"></a>/**
<a name="line69"></a> * Compares coordinates for equality.
<a name="line70"></a> * @param {goog.math.Coordinate} a A Coordinate.
<a name="line71"></a> * @param {goog.math.Coordinate} b A Coordinate.
<a name="line72"></a> * @return {boolean} True iff the coordinates are equal, or if both are null.
<a name="line73"></a> */
<a name="line74"></a>goog.math.Coordinate.equals = function(a, b) {
<a name="line75"></a>  if (a == b) {
<a name="line76"></a>    return true;
<a name="line77"></a>  }
<a name="line78"></a>  if (!a || !b) {
<a name="line79"></a>    return false;
<a name="line80"></a>  }
<a name="line81"></a>  return a.x == b.x &amp;&amp; a.y == b.y;
<a name="line82"></a>};
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * Returns the distance between two coordinates.
<a name="line87"></a> * @param {!goog.math.Coordinate} a A Coordinate.
<a name="line88"></a> * @param {!goog.math.Coordinate} b A Coordinate.
<a name="line89"></a> * @return {number} The distance between {@code a} and {@code b}.
<a name="line90"></a> */
<a name="line91"></a>goog.math.Coordinate.distance = function(a, b) {
<a name="line92"></a>  var dx = a.x - b.x;
<a name="line93"></a>  var dy = a.y - b.y;
<a name="line94"></a>  return Math.sqrt(dx * dx + dy * dy);
<a name="line95"></a>};
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * Returns the magnitude of a coordinate.
<a name="line100"></a> * @param {!goog.math.Coordinate} a A Coordinate.
<a name="line101"></a> * @return {number} The distance between the origin and {@code a}.
<a name="line102"></a> */
<a name="line103"></a>goog.math.Coordinate.magnitude = function(a) {
<a name="line104"></a>  return Math.sqrt(a.x * a.x + a.y * a.y);
<a name="line105"></a>};
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/**
<a name="line109"></a> * Returns the angle from the origin to a coordinate.
<a name="line110"></a> * @param {!goog.math.Coordinate} a A Coordinate.
<a name="line111"></a> * @return {number} The angle, in degrees, clockwise from the positive X
<a name="line112"></a> *     axis to {@code a}.
<a name="line113"></a> */
<a name="line114"></a>goog.math.Coordinate.azimuth = function(a) {
<a name="line115"></a>  return goog.math.angle(0, 0, a.x, a.y);
<a name="line116"></a>};
<a name="line117"></a>
<a name="line118"></a>
<a name="line119"></a>/**
<a name="line120"></a> * Returns the squared distance between two coordinates. Squared distances can
<a name="line121"></a> * be used for comparisons when the actual value is not required.
<a name="line122"></a> *
<a name="line123"></a> * Performance note: eliminating the square root is an optimization often used
<a name="line124"></a> * in lower-level languages, but the speed difference is not nearly as
<a name="line125"></a> * pronounced in JavaScript (only a few percent.)
<a name="line126"></a> *
<a name="line127"></a> * @param {!goog.math.Coordinate} a A Coordinate.
<a name="line128"></a> * @param {!goog.math.Coordinate} b A Coordinate.
<a name="line129"></a> * @return {number} The squared distance between {@code a} and {@code b}.
<a name="line130"></a> */
<a name="line131"></a>goog.math.Coordinate.squaredDistance = function(a, b) {
<a name="line132"></a>  var dx = a.x - b.x;
<a name="line133"></a>  var dy = a.y - b.y;
<a name="line134"></a>  return dx * dx + dy * dy;
<a name="line135"></a>};
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Returns the difference between two coordinates as a new
<a name="line140"></a> * goog.math.Coordinate.
<a name="line141"></a> * @param {!goog.math.Coordinate} a A Coordinate.
<a name="line142"></a> * @param {!goog.math.Coordinate} b A Coordinate.
<a name="line143"></a> * @return {!goog.math.Coordinate} A Coordinate representing the difference
<a name="line144"></a> *     between {@code a} and {@code b}.
<a name="line145"></a> */
<a name="line146"></a>goog.math.Coordinate.difference = function(a, b) {
<a name="line147"></a>  return new goog.math.Coordinate(a.x - b.x, a.y - b.y);
<a name="line148"></a>};
<a name="line149"></a>
<a name="line150"></a>
<a name="line151"></a>/**
<a name="line152"></a> * Returns the sum of two coordinates as a new goog.math.Coordinate.
<a name="line153"></a> * @param {!goog.math.Coordinate} a A Coordinate.
<a name="line154"></a> * @param {!goog.math.Coordinate} b A Coordinate.
<a name="line155"></a> * @return {!goog.math.Coordinate} A Coordinate representing the sum of the two
<a name="line156"></a> *     coordinates.
<a name="line157"></a> */
<a name="line158"></a>goog.math.Coordinate.sum = function(a, b) {
<a name="line159"></a>  return new goog.math.Coordinate(a.x + b.x, a.y + b.y);
<a name="line160"></a>};
<a name="line161"></a>
<a name="line162"></a>
<a name="line163"></a>/**
<a name="line164"></a> * Rounds the x and y fields to the next larger integer values.
<a name="line165"></a> * @return {!goog.math.Coordinate} This coordinate with ceil&#39;d fields.
<a name="line166"></a> */
<a name="line167"></a>goog.math.Coordinate.prototype.ceil = function() {
<a name="line168"></a>  this.x = Math.ceil(this.x);
<a name="line169"></a>  this.y = Math.ceil(this.y);
<a name="line170"></a>  return this;
<a name="line171"></a>};
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>/**
<a name="line175"></a> * Rounds the x and y fields to the next smaller integer values.
<a name="line176"></a> * @return {!goog.math.Coordinate} This coordinate with floored fields.
<a name="line177"></a> */
<a name="line178"></a>goog.math.Coordinate.prototype.floor = function() {
<a name="line179"></a>  this.x = Math.floor(this.x);
<a name="line180"></a>  this.y = Math.floor(this.y);
<a name="line181"></a>  return this;
<a name="line182"></a>};
<a name="line183"></a>
<a name="line184"></a>
<a name="line185"></a>/**
<a name="line186"></a> * Rounds the x and y fields to the nearest integer values.
<a name="line187"></a> * @return {!goog.math.Coordinate} This coordinate with rounded fields.
<a name="line188"></a> */
<a name="line189"></a>goog.math.Coordinate.prototype.round = function() {
<a name="line190"></a>  this.x = Math.round(this.x);
<a name="line191"></a>  this.y = Math.round(this.y);
<a name="line192"></a>  return this;
<a name="line193"></a>};
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>/**
<a name="line197"></a> * Translates this box by the given offsets. If a {@code goog.math.Coordinate}
<a name="line198"></a> * is given, then the x and y values are translated by the coordinate&#39;s x and y.
<a name="line199"></a> * Otherwise, x and y are translated by {@code tx} and {@code opt_ty}
<a name="line200"></a> * respectively.
<a name="line201"></a> * @param {number|goog.math.Coordinate} tx The value to translate x by or the
<a name="line202"></a> *     the coordinate to translate this coordinate by.
<a name="line203"></a> * @param {number=} opt_ty The value to translate y by.
<a name="line204"></a> * @return {!goog.math.Coordinate} This coordinate after translating.
<a name="line205"></a> */
<a name="line206"></a>goog.math.Coordinate.prototype.translate = function(tx, opt_ty) {
<a name="line207"></a>  if (tx instanceof goog.math.Coordinate) {
<a name="line208"></a>    this.x += tx.x;
<a name="line209"></a>    this.y += tx.y;
<a name="line210"></a>  } else {
<a name="line211"></a>    this.x += tx;
<a name="line212"></a>    if (goog.isNumber(opt_ty)) {
<a name="line213"></a>      this.y += opt_ty;
<a name="line214"></a>    }
<a name="line215"></a>  }
<a name="line216"></a>  return this;
<a name="line217"></a>};
<a name="line218"></a>
<a name="line219"></a>
<a name="line220"></a>/**
<a name="line221"></a> * Scales this coordinate by the given scale factors. The x and y values are
<a name="line222"></a> * scaled by {@code sx} and {@code opt_sy} respectively.  If {@code opt_sy}
<a name="line223"></a> * is not given, then {@code sx} is used for both x and y.
<a name="line224"></a> * @param {number} sx The scale factor to use for the x dimension.
<a name="line225"></a> * @param {number=} opt_sy The scale factor to use for the y dimension.
<a name="line226"></a> * @return {!goog.math.Coordinate} This coordinate after scaling.
<a name="line227"></a> */
<a name="line228"></a>goog.math.Coordinate.prototype.scale = function(sx, opt_sy) {
<a name="line229"></a>  var sy = goog.isNumber(opt_sy) ? opt_sy : sx;
<a name="line230"></a>  this.x *= sx;
<a name="line231"></a>  this.y *= sy;
<a name="line232"></a>  return this;
<a name="line233"></a>};
</pre>


</body>
</html>
